/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.server.impl;


import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import me.zhengjie.dao.InventoryTaskRecordDao;
import me.zhengjie.dao.dto.InventoryTaskRecordQueryCriteria;
import me.zhengjie.mapper.InventoryTaskRecordMapper;
import me.zhengjie.server.InventoryTaskRecordService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;


/**
* @description 服务实现
* @author laoxu
* @date 2025-07-04
**/
@Service
@RequiredArgsConstructor
public class InventoryTaskRecordServiceImpl extends ServiceImpl<InventoryTaskRecordMapper, InventoryTaskRecordDao> implements InventoryTaskRecordService {

    private final InventoryTaskRecordMapper inventoryTaskRecordMapper;

    @Override
    public PageResult<InventoryTaskRecordDao> queryAll(InventoryTaskRecordQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(inventoryTaskRecordMapper.findAll(criteria, page));
    }

    @Override
    public List<InventoryTaskRecordDao> queryAll(InventoryTaskRecordQueryCriteria criteria){
        return inventoryTaskRecordMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(InventoryTaskRecordDao resources) {
        inventoryTaskRecordMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InventoryTaskRecordDao resources) {
        InventoryTaskRecordDao inventoryTaskRecordDao = getById(resources.getId());
        inventoryTaskRecordDao.copy(resources);
        inventoryTaskRecordMapper.updateById(inventoryTaskRecordDao);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        inventoryTaskRecordMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<InventoryTaskRecordDao> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InventoryTaskRecordDao inventoryTaskRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务单号", inventoryTaskRecord.getTaskId());
            map.put("任务状态", inventoryTaskRecord.getTaskStatus());
            map.put("盘点类型（全盘，抽盘，动碰盘）", inventoryTaskRecord.getInventoryType());
            map.put("载具码", inventoryTaskRecord.getContainerCode());
            map.put("物料条码", inventoryTaskRecord.getItemBarCode());
            map.put("仓库", inventoryTaskRecord.getFloor());
            map.put("建单账号", inventoryTaskRecord.getCreateName());
            map.put("建单时间", inventoryTaskRecord.getCreateDate());
            map.put("开始时间", inventoryTaskRecord.getStartDate());
            map.put("完成时间", inventoryTaskRecord.getCompleteDate());
            map.put("物料编号", inventoryTaskRecord.getMaterialCode());
            map.put("物料名称", inventoryTaskRecord.getMaterialName());
            map.put("入厂时间", inventoryTaskRecord.getEntryTime());
            map.put("物料数量", inventoryTaskRecord.getInventoryQuantity());
            map.put("盘点账号", inventoryTaskRecord.getStartName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}